<?php

namespace App\Admin\Controllers;

use App\Models\FreeUser;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class FreeUserController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('Index')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new FreeUser);
        $grid->model()->orderBy('id','desc');
        $grid->model()->whereNotNull('self_portrait');
        $grid->model()->whereNotNull('identity_front');
        $grid->model()->whereNotNull('identity_reverse');

        $grid->id('ID');
        $grid->user_name('手机号');
        $grid->u_name('姓名');
        $grid->identity('身份证号');
        // $grid->relation('关系');
        // $grid->linkman_name('联系人');
        // $grid->linkman_tel('联系人电话');
        $grid->self_portrait('手持证件照')->display(function($value){
            $src = '/uploads/free_user/'.$value;
            return "<a href='".$src."' target='_blank'><img src='".$src."' style='max-width:100px;max-height:100px' class='img img-thumbnail'></a>";
        });
        $grid->identity_front('身份证正面')->display(function($value){
            $src = '/uploads/free_user/'.$value;
            return "<a href='".$src."' target='_blank'><img src='".$src."' style='max-width:100px;max-height:100px' class='img img-thumbnail'></a>";
        });
        $grid->identity_reverse('身份证反面')->display(function($value){
            $src = '/uploads/free_user/'.$value;
            return "<a href='".$src."' target='_blank'><img src='".$src."' style='max-width:100px;max-height:100px' class='img img-thumbnail'></a>";
        });
        // $grid->bank_name('银行卡持有人');
        // $grid->bank_card('银行卡号');
        // $grid->bank_tel('绑定手机');
        // $grid->create_time('创建时间')->display(function(){
        //     return date('Y-m-d H:i:s',$this->create_time);
        // });
        $grid->actions(function ($actions) {
            // 当前行的数据数组
            $iden = $actions->row->identity;
            $sub_iden1 = substr($iden,16,1);
            $sub_iden2 = substr($iden,6,4); 
            
            if (($sub_iden1%2 !== 0) && $sub_iden2<1989) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->disableView();
            }
        });
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->like('u_name', '姓名');
            $filter->like('user_name', '手机号');
            

        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(FreeUser::findOrFail($id));

        $show->id('User id');
        $show->user_name('User name');
        $show->pass_word('Pass word');
        $show->u_name('U name');
        $show->identity('Identity');
        $show->relation('Relation');
        $show->linkman_name('Linkman name');
        $show->linkman_tel('Linkman tel');
        $show->self_portrait('Self portrait');
        $show->identity_front('Identity front');
        $show->identity_reverse('Identity reverse');
        $show->bank_name('Bank name');
        $show->bank_card('Bank card');
        $show->bank_tel('Bank tel');
        $show->create_time('Create time');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new FreeUser);

        $form->number('id', 'User id');
        $form->text('user_name', 'User name');
        $form->text('pass_word', 'Pass word');
        $form->text('u_name', 'U name');
        $form->text('identity', 'Identity');
        $form->text('relation', 'Relation');
        $form->text('linkman_name', 'Linkman name');
        $form->text('linkman_tel', 'Linkman tel');
        $form->text('self_portrait', 'Self portrait');
        $form->text('identity_front', 'Identity front');
        $form->text('identity_reverse', 'Identity reverse');
        $form->text('bank_name', 'Bank name');
        $form->text('bank_card', 'Bank card');
        $form->text('bank_tel', 'Bank tel');
        $form->text('create_time', 'Create time');

        return $form;
    }
}
